<?php

declare (strict_types=1);
namespace cms\exam\model;

use Carbon\Carbon;
use Hyperf\Database\Model\Relations\HasOne;

/**
 * @property int $id 
 * @property string $name 名称标识
 * @property int $status 状态,1|正常、2|禁用
 * @property Carbon $created_at 创建时间
 * @property Carbon $updated_at 更新时间
 * @property Carbon $deleted_at 删除时间
 */
class TopicGroupModel extends CommonModel
{
    /**
     * @var string
     */
    protected $table = 'exam_topic_group';

    public $appends = [
        'select_single_count',
    ];

    public function getSelectSingleCountAttribute(): array
    {
        $query = make(TopicModel::class)->where('group_id', $this->id)->where('type', 'select_single');
        return [
            'total' => $query->count('id'),
            'level_1' => $query->where('level', 1)->count('id'),
            'level_2' => $query->where('level', 2)->count('id'),
            'level_3' => $query->where('level', 3)->count('id'),
        ];
    }

    /**
     * 关联站点
     * @return HasOne
     */
    public function app(): HasOne
    {
        return $this->hasOne(AppModel::class, 'id', 'app_id');
    }
}